package com.dongdongshop.shiro;


import com.dongdongshop.pojo.User;
import com.dongdongshop.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class LoginRealm extends AuthorizingRealm {


    @DubboReference
    UserService us;




    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


    //登录认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;//接收传过来的token,多态向下转型
        User user = us.byuser(token.getUsername());
        if(user==null){
            return null;//如果person不存在用户名不存在
        }

        //返回登录成功
        //四个参数分别为：登录成功的用户实体类,用户密码,盐值,登陆成功的用户名
        return new SimpleAuthenticationInfo(user,user.getPassword(), ByteSource.Util.bytes(user.getSalt()),user.getUsername());
    }
}
